const webpack 						= require('webpack');
const HtmlWebpackPlugin 			= require('html-webpack-plugin');
const glob = require('glob');
const path = require('path');

const setMPA = () => {
	const entry = {};
  	const htmlWebpackPlugins = [];
  	const entryFiles = glob.sync(path.resolve(__dirname, '../src/*/main.js'));
  	for(let item of entryFiles){
  		const pageName = item.match(/src\/(.*)\/main.js/)[1];
	    entry[pageName] = item;
	    htmlWebpackPlugins.push(
	    	new HtmlWebpackPlugin({
	    		chunks:[pageName],
				filename:`${pageName}.html`,
				template:path.resolve(__dirname,`../src/${pageName}/index.html`),
			}),
	    )
  	}
    return {
    	entry,
    	htmlWebpackPlugins
  	}
}

const { entry, htmlWebpackPlugins } = setMPA();

module.exports = {
	entry,
	devtool: 'eval-source-map',
	module:{
		rules:[
			{
				test: /\.m?js$/,
				include: path.resolve(__dirname,'../src'),
				exclude: /node_modules/,
				use:[
					{
						loader:'babel-loader',
					}
				]
			}
		]
	},
	plugins:[
		new webpack.HotModuleReplacementPlugin(),
	].concat(htmlWebpackPlugins),
	resolve:{
		extensions:['.js'],
		alias:{
			// 'vue': 'vue/dist/vue.esm.js',
			'@':path.resolve(__dirname,'../src')
		}
	},
	devServer:{
		progress:true,					//进度条
		inline:true,					//打包后加入一个websocket客户端
	    hot:true,						//热加载
	    contentBase: path.resolve(__dirname,'../src'),	//开发服务运行时的文件根目录
	    host: 'localhost',				//主机地址
	    port: 5000,						//端口号
	}
}